In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import zipfile
from tensorflow.keras import backend as K
import cv2
import glob
#from utils import *
from pathlib import Path
import tensorflow as tf
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers,regularizers,optimizers
from tensorflow.keras import callbacks
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
2024-08-19 19:19:29.333774: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-08-19 19:19:29.333888: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-08-19 19:19:29.465479: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
In [2]:
def dice_coefficients(y_true, y_pred, smooth=100):
    """
    Computes the Dice similarity coefficient between the true and predicted values.

    The Dice coefficient is a statistical metric used to gauge the similarity of two samples.
    With a range from 0 to 1, the Dice coefficient is 1 when the two samples are identical and
    0 when they share no elements. A smoothing term is included to prevent division by zero.

    This function is typically used as a loss function for binary segmentation tasks,
    where the true and predicted values are binary masks of the same size.

    Parameters:
    -----------
    y_true : tensor
        The ground truth values. Typically a binary mask.

    y_pred : tensor
        The predicted values. Typically a binary mask.

    smooth : float, optional
        A smoothing factor to prevent division by zero. Default is 100.

    Returns:
    --------
    float
        The Dice similarity coefficient between the true and predicted values.

    Note:
    -----
    The inputs are flattened to 1D tensors before computation to handle
    both single and multi-channel inputs.
    """

    y_true_flatten = K.flatten(y_true)
    y_pred_flatten = K.flatten(y_pred)

    intersection = K.sum(y_true_flatten * y_pred_flatten)
    union = K.sum(y_true_flatten) + K.sum(y_pred_flatten)
    return (2 * intersection + smooth) / (union + smooth)


def dice_coefficients_loss(y_true, y_pred, smooth=100):
    """
    The Dice loss function for image segmentation models.

    The Dice loss is a measure of the overlap between the prediction (y_pred)
    and the ground truth (y_true). It ranges from 0 to 1, where a Dice loss
    of 1 indicates perfect overlap (i.e., a perfect segmentation), while a Dice
    loss of 0 indicates no overlap.

    The 'smooth' parameter is a small constant added to the numerator and
    denominator of the Dice coefficient to avoid division by zero errors
    and to stabilize the training.

    Parameters:
    y_true (tf.Tensor): Ground truth. Tensor of the same shape as y_pred.
    y_pred (tf.Tensor): Model prediction. Tensor output from the model.
    smooth (float, optional): A smoothing constant to avoid division by zero errors. Default is 100.

    Returns:
    float: The computed Dice loss.

    Why the negative sign here i.e. -dice_coefficients

    most optimization algorithms are designed to minimize a function rather than maximize it. Therefore, to convert the maximization problem to a minimization problem, we take the negative of the Dice coefficient. As a result, when the Dice coefficient is high (which is good), the loss is low, and when the Dice coefficient is low (which is bad), the loss is high. This allows the model to use standard optimization techniques to find the best parameters.

    """

    return -dice_coefficients(y_true, y_pred, smooth)


def iou(y_true, y_pred, smooth=100):
    """
    Calculates the Intersection over Union (IoU) between the true and predicted values.

    IoU, also known as the Jaccard Index, is a metric used to quantify the percent overlap
    between the target mask and our prediction output. It's often used in segmentation problems
    to evaluate the quality of predictions.

    This function is generally used for evaluating segmentation tasks where the true and
    predicted outputs are binary masks of the same size.

    Parameters:
    -----------
    y_true : tensor
        The ground truth values. Typically a binary mask.

    y_pred : tensor
        The predicted values. Typically a binary mask.

    smooth : float, optional
        A smoothing factor to prevent division by zero. Default is 100.

    Returns:
    --------
    float
        The Intersection over Union (IoU) between the true and predicted values.

    Note:
    -----
    The inputs are not flattened to 1D tensors before computation because Keras backend
    operations automatically broadcast the tensors to the appropriate shapes.
    """
    intersection = K.sum(y_true * y_pred)
    sum = K.sum(y_true + y_pred)
    iou = (intersection + smooth) / (sum - intersection + smooth)
    return iou

""" Why intersection = K.sum(y_true * y_pred) in above

The line intersection = K.sum(y_true * y_pred) is calculating the intersection of two sets, where the sets are represented as binary masks (for a segmentation problem). The intersection is basically the overlapping region of the two sets.

This is done by performing an element-wise multiplication between the true values (y_true) and the predicted values (y_pred). In the context of binary masks, this operation essentially counts the number of pixels where both the true and predicted masks are 1 (indicating a positive class).

This is because in a binary mask, a pixel value of 1 denotes the presence of the object of interest (in a segmentation task, for instance), and a pixel value of 0 denotes the background or absence of the object. Thus, when both y_true and y_pred are 1, it means that both the ground truth and the prediction agree that there is an object at that particular pixel location.

In the context of binary masks for a segmentation problem, the masks represent the region of interest in an image, where '1' denotes the presence of an object (or class) and '0' denotes the absence of that object (or background).

When we multiply these masks element-wise (y_true * y_pred), we are looking for places where both masks agree that there is an object of interest. If both y_true and y_pred are 1 at a given pixel, then the product is 1, indicating an intersection at that pixel. If either of them is 0 at a given pixel, then the product is 0, indicating no intersection.

By summing up all these products (K.sum(y_true * y_pred)), we are effectively counting the number of pixels where both the ground truth (y_true) and the prediction (y_pred) agree that there is an object of interest. This is the intersection of the ground truth and prediction.


The K.sum() operation then sums up all these overlapping '1's to give a single number representing the total intersection, or overlap, between the true and predicted values.
"""


def jaccard_distance(y_true, y_pred):
    """
    Function to compute the Jaccard distance between the true labels and the predicted labels.

    The Jaccard distance, which is a measure of dissimilarity between sets, is computed as one minus
    the intersection over union (IoU) of the sets. A lower Jaccard distance between the predicted labels
    and the true labels indicates a better model fit.

    Parameters:
    y_true (np.array): The ground truth label array (binary mask).
    y_pred (np.array): The predicted label array (binary mask).

    Returns:
    float: The Jaccard distance between the ground truth labels and the predicted labels.
    """

    y_true_flatten = K.flatten(y_true)
    y_pred_flatten = K.flatten(y_pred)
    return -iou(y_true_flatten, y_pred_flatten)
In [3]:
def explore_files(dirpath):
  for dirpath,dir_names,file_names in os.walk(dirpath):
       print(f"There are {len(dir_names)} directories and {len(file_names)} images in '{dirpath}'.")
  print()
In [4]:
explore_files('/kaggle/input/lgg-mri-segmentation/kaggle_3m')
There are 110 directories and 2 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m'.
There are 0 directories and 116 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7010_19860307'.
There are 0 directories and 74 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_8162_19961029'.
There are 0 directories and 100 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_A4MT_20020212'.
There are 0 directories and 52 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_5964_20010511'.
There are 0 directories and 70 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_A5TS_19970726'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7692_19960724'.
There are 0 directories and 76 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5849_19950405'.
There are 0 directories and 146 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_A60K_20040224'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7475_19970918'.
There are 0 directories and 96 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_6691_20020405'.
There are 0 directories and 50 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7684_19950816'.
There are 0 directories and 48 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6188_20010812'.
There are 0 directories and 42 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7694_19950404'.
There are 0 directories and 76 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_A5TR_19970726'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7300_19910814'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7018_19911220'.
There are 0 directories and 70 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7301_19911112'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7302_19911203'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8018_19970411'.
There are 0 directories and 52 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_6692_20020606'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5854_19951104'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7299_19910417'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_A5RC_19990831'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8105_19980826'.
There are 0 directories and 46 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8563_19981209'.
There are 0 directories and 176 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_A61A_20000127'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_4944_20010208'.
There are 0 directories and 96 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_7643_20021104'.
There are 0 directories and 74 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_8163_19961119'.
There are 0 directories and 44 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6669_20020102'.
There are 0 directories and 98 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7013_19860523'.
There are 0 directories and 120 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_8189_20030516'.
There are 0 directories and 44 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8111_19980330'.
There are 0 directories and 48 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_5396_20010302'.
There are 0 directories and 68 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7294_19890104'.
There are 0 directories and 56 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7879_19981009'.
There are 0 directories and 48 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_EZ_7264_20010816'.
There are 0 directories and 74 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_8164_19970111'.
There are 0 directories and 44 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7860_19960513'.
There are 0 directories and 160 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7881_19981015'.
There are 0 directories and 114 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_6400_19830518'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7686_19950629'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_6688_20020215'.
There are 0 directories and 102 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_6401_19831001'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_4943_20000902'.
There are 0 directories and 62 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_A5TW_19980228'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7473_19970826'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5853_19950823'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6290_20000917'.
There are 0 directories and 106 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_6399_19830416'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_4942_19970222'.
There are 0 directories and 142 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5872_19950223'.
There are 0 directories and 60 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7616_19940813'.
There are 0 directories and 76 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7019_19940908'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5871_19941206'.
There are 0 directories and 52 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6666_20011109'.
There are 0 directories and 102 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_7637_20000922'.
There are 0 directories and 44 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_5397_20010315'.
There are 0 directories and 46 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_4941_19960909'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7874_19950902'.
There are 0 directories and 106 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_6404_19850629'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_8166_19970322'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7605_19950916'.
There are 0 directories and 102 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_5962_20000626'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7856_19950831'.
There are 0 directories and 120 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_6405_19851005'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5852_19950709'.
There are 0 directories and 44 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8114_19981030'.
There are 0 directories and 54 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_7634_20000128'.
There are 0 directories and 48 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6665_20010817'.
There are 0 directories and 42 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7855_19951020'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7602_19951103'.
There are 0 directories and 74 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_8167_19970402'.
There are 0 directories and 80 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7309_19960831'.
There are 0 directories and 76 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5874_19950510'.
There are 0 directories and 116 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_6407_19860514'.
There are 0 directories and 48 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7690_19960312'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7884_19980913'.
There are 0 directories and 52 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5855_19951217'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7298_19910324'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_A4MU_20030903'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6667_20011105'.
There are 0 directories and 60 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7877_19980917'.
There are 0 directories and 112 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_A5TT_19980318'.
There are 0 directories and 96 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_6689_20020326'.
There are 0 directories and 176 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_A61B_19991127'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8107_19980708'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_A5TY_19970709'.
There are 0 directories and 56 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7608_19940304'.
There are 0 directories and 50 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6186_20000601'.
There are 0 directories and 80 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_5851_19950428'.
There are 0 directories and 42 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7693_19950520'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_8165_19970205'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_8168_19970503'.
There are 0 directories and 104 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7008_19830723'.
There are 0 directories and 64 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7882_19970125'.
There are 0 directories and 72 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7304_19930325'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_5393_19990606'.
There are 0 directories and 120 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7014_19860618'.
There are 0 directories and 44 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8106_19970727'.
There are 0 directories and 56 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_6668_20011025'.
There are 0 directories and 42 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_7680_19970202'.
There are 0 directories and 120 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_FG_6690_20020226'.
There are 0 directories and 112 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_6408_19860521'.
There are 0 directories and 46 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_A5TU_19980312'.
There are 0 directories and 56 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_A616_19991226'.
There are 0 directories and 40 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_CS_5395_19981004'.
There are 0 directories and 42 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_HT_8113_19930809'.
There are 0 directories and 76 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_A5TP_19970614'.
There are 0 directories and 84 images in '/kaggle/input/lgg-mri-segmentation/kaggle_3m/TCGA_DU_7306_19930512'.

In [5]:
def create_image_mask_df(dirpath):
    images=[]
    masks=[]
    #extract masks
    masks=sorted(glob.glob(dirpath+'/*/*_mask*'))
   #extract images
    for mask in masks:
      images.append(mask.replace('_mask',''))


    df=pd.DataFrame(columns=['file_path','mask'])
    df['file_path']=images
    df['mask']=masks

    return df
In [6]:
df=create_image_mask_df('/kaggle/input/lgg-mri-segmentation/kaggle_3m')
In [7]:
df
Out[7]:
file_path mask
0 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
1 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
2 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
3 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
4 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
... ... ...
3924 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
3925 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
3926 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
3927 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...
3928 /kaggle/input/lgg-mri-segmentation/kaggle_3m/T... /kaggle/input/lgg-mri-segmentation/kaggle_3m/T...

3929 rows × 2 columns

Show Some Samples!¶

In [8]:
def show_image_sample(df):
# Select 8 random indices from the DataFrame
  random_indices = np.random.choice(df.index, size=8, replace=False)
  random_images = df.iloc[random_indices]['file_path'].values
  random_masks = df.iloc[random_indices]['mask'].values



  fig = plt.figure(figsize=(12, 12))
  plt.suptitle('Randomly Brain images with their mask')
  for i in range(8):
    image = cv2.imread(random_images[i])
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    mask = cv2.imread(random_masks[i],cv2.IMREAD_GRAYSCALE)

    plt.subplot(4,4,2*i+1)
    plt.imshow(image)
    plt.axis('on')
    plt.grid(True)    
    
    plt.subplot(4,4,2*i+2)
    plt.imshow(mask, cmap='gray')
    plt.axis('on')    
    # Overlay the mask with some transparency
    
   
    plt.grid(True)
  plt.tight_layout()
  plt.show()

  
show_image_sample(df)
In [9]:
train_df,test_df=train_test_split(df,test_size=0.2,random_state=42)
train_df,val_df=train_test_split(train_df,test_size=0.2,random_state=42)
print(f"Train: {len(train_df)} Val: {len(val_df)} Test: {len(test_df)}")
Train: 2514 Val: 629 Test: 786
In [10]:
def normalize_and_diagnose(img,mask):
      img=img/255.0
      mask=mask/255.0
      mask[mask>0.5]=1
      mask[mask<=0.5]=0
      return img,mask

def train_generator(
    data_frame,
    batch_size,

    image_color_mode="rgb",
    mask_color_mode="grayscale",
    image_save_prefix="image",
    mask_save_prefix="mask",
    save_to_dir=None,
    target_size=(256, 256),
    seed=1,
):
    """
    can generate image and mask at the same time use the same seed for
    image_datagen and mask_datagen to ensure the transformation for image
    and mask is the same
    """
    image_datagen = ImageDataGenerator()
    mask_datagen = ImageDataGenerator()

    image_generator = image_datagen.flow_from_dataframe(
        data_frame,
        x_col="file_path",
        class_mode=None,
        color_mode=image_color_mode,
        target_size=target_size,
        batch_size=batch_size,
        save_to_dir=save_to_dir,
        save_prefix=image_save_prefix,
        seed=seed,
    )

    mask_generator = mask_datagen.flow_from_dataframe(
        data_frame,
        x_col="mask",
        class_mode=None,
        color_mode=mask_color_mode,
        target_size=target_size,
        batch_size=batch_size,
        save_to_dir=save_to_dir,
        save_prefix=mask_save_prefix,
        seed=seed,
    )



    train_gen=zip(image_generator,mask_generator)

    for (img,mask) in train_gen:
        img, mask = normalize_and_diagnose(img, mask)
        yield (img, mask)

Set Constants¶

In [11]:
EPOCHS = 50
BATCH_SIZE = 32
learning_rate = 1e-4
w,h=256,256
In [12]:
train_gen=train_generator(train_df,batch_size=BATCH_SIZE,target_size=(w,h))
val_gen=train_generator(val_df,batch_size=BATCH_SIZE,target_size=(w,h))
test_gen=train_generator(test_df,batch_size=BATCH_SIZE,target_size=(w,h))

Unet Architcutre¶

In [13]:
def double_conv_block(inputs,num_filters):

  x=layers.Conv2D(num_filters,3,padding='same')(inputs)
  x=layers.BatchNormalization()(x)
  x=layers.Activation('relu')(x)

  x=layers.Conv2D(num_filters,3,padding='same')(x)
  x=layers.BatchNormalization()(x)
  x=layers.Activation('relu')(x)

  return x



 ## upsampling
def encoder_block(inputs,num_filters):
     x=double_conv_block(inputs,num_filters)
     p= layers.MaxPooling2D((2,2))(x)

     return x,p



def decoder_block(inputs,skip_connection,num_filters):
    x=layers.Conv2DTranspose(num_filters,2,strides=2,padding='same')(inputs)

    h_diff = skip_connection.shape[1] - x.shape[1]
    w_diff = skip_connection.shape[2] - x.shape[2]

    if h_diff != 0 or w_diff != 0:
        skip_connection = layers.Cropping2D(((h_diff // 2, h_diff - h_diff // 2),
                                             (w_diff // 2, w_diff - w_diff // 2)))(skip_connection)

    x = layers.Concatenate()([x, skip_connection])
    x = double_conv_block(x, num_filters)
    return x



def Unet(input_shape):
    inputs=layers.Input(input_shape)

    #encoder block
    b1,p1=encoder_block(inputs,64)
    b2,p2=encoder_block(p1,128)
    b3,p3=encoder_block(p2,256)
    b4,p4=encoder_block(p3,512)


    #bottle neck
    b=double_conv_block(p4,1024)


    #decoder block
    d1=decoder_block(b,b4,512)
    d2=decoder_block(d1,b3,256)
    d3=decoder_block(d2,b2,128)
    d4=decoder_block(d3,b1,64)

    #output
    outputs=layers.Conv2D(1,1,padding='same',activation='sigmoid')(d4)



    model=tf.keras.models.Model(inputs,outputs,name='unet')

    return model


# if __name__ == "__main__":
#     input_shape = (572, 572, 3)
#     model = Unet(input_shape)
#     model.summary()


model=Unet((w,h,3))
model.summary()
Model: "unet"
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
┃ Layer (type)        ┃ Output Shape      ┃    Param # ┃ Connected to      ┃
┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
│ input_layer         │ (None, 256, 256,  │          0 │ -                 │
│ (InputLayer)        │ 3)                │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d (Conv2D)     │ (None, 256, 256,  │      1,792 │ input_layer[0][0] │
│                     │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalization │ (None, 256, 256,  │        256 │ conv2d[0][0]      │
│ (BatchNormalizatio… │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation          │ (None, 256, 256,  │          0 │ batch_normalizat… │
│ (Activation)        │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_1 (Conv2D)   │ (None, 256, 256,  │     36,928 │ activation[0][0]  │
│                     │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 256, 256,  │        256 │ conv2d_1[0][0]    │
│ (BatchNormalizatio… │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_1        │ (None, 256, 256,  │          0 │ batch_normalizat… │
│ (Activation)        │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ max_pooling2d       │ (None, 128, 128,  │          0 │ activation_1[0][… │
│ (MaxPooling2D)      │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_2 (Conv2D)   │ (None, 128, 128,  │     73,856 │ max_pooling2d[0]… │
│                     │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 128, 128,  │        512 │ conv2d_2[0][0]    │
│ (BatchNormalizatio… │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_2        │ (None, 128, 128,  │          0 │ batch_normalizat… │
│ (Activation)        │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_3 (Conv2D)   │ (None, 128, 128,  │    147,584 │ activation_2[0][… │
│                     │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 128, 128,  │        512 │ conv2d_3[0][0]    │
│ (BatchNormalizatio… │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_3        │ (None, 128, 128,  │          0 │ batch_normalizat… │
│ (Activation)        │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ max_pooling2d_1     │ (None, 64, 64,    │          0 │ activation_3[0][… │
│ (MaxPooling2D)      │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_4 (Conv2D)   │ (None, 64, 64,    │    295,168 │ max_pooling2d_1[… │
│                     │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 64, 64,    │      1,024 │ conv2d_4[0][0]    │
│ (BatchNormalizatio… │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_4        │ (None, 64, 64,    │          0 │ batch_normalizat… │
│ (Activation)        │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_5 (Conv2D)   │ (None, 64, 64,    │    590,080 │ activation_4[0][… │
│                     │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 64, 64,    │      1,024 │ conv2d_5[0][0]    │
│ (BatchNormalizatio… │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_5        │ (None, 64, 64,    │          0 │ batch_normalizat… │
│ (Activation)        │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ max_pooling2d_2     │ (None, 32, 32,    │          0 │ activation_5[0][… │
│ (MaxPooling2D)      │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_6 (Conv2D)   │ (None, 32, 32,    │  1,180,160 │ max_pooling2d_2[… │
│                     │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 32, 32,    │      2,048 │ conv2d_6[0][0]    │
│ (BatchNormalizatio… │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_6        │ (None, 32, 32,    │          0 │ batch_normalizat… │
│ (Activation)        │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_7 (Conv2D)   │ (None, 32, 32,    │  2,359,808 │ activation_6[0][… │
│                     │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 32, 32,    │      2,048 │ conv2d_7[0][0]    │
│ (BatchNormalizatio… │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_7        │ (None, 32, 32,    │          0 │ batch_normalizat… │
│ (Activation)        │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ max_pooling2d_3     │ (None, 16, 16,    │          0 │ activation_7[0][… │
│ (MaxPooling2D)      │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_8 (Conv2D)   │ (None, 16, 16,    │  4,719,616 │ max_pooling2d_3[… │
│                     │ 1024)             │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 16, 16,    │      4,096 │ conv2d_8[0][0]    │
│ (BatchNormalizatio… │ 1024)             │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_8        │ (None, 16, 16,    │          0 │ batch_normalizat… │
│ (Activation)        │ 1024)             │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_9 (Conv2D)   │ (None, 16, 16,    │  9,438,208 │ activation_8[0][… │
│                     │ 1024)             │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 16, 16,    │      4,096 │ conv2d_9[0][0]    │
│ (BatchNormalizatio… │ 1024)             │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_9        │ (None, 16, 16,    │          0 │ batch_normalizat… │
│ (Activation)        │ 1024)             │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_transpose    │ (None, 32, 32,    │  2,097,664 │ activation_9[0][… │
│ (Conv2DTranspose)   │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ concatenate         │ (None, 32, 32,    │          0 │ conv2d_transpose… │
│ (Concatenate)       │ 1024)             │            │ activation_7[0][… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_10 (Conv2D)  │ (None, 32, 32,    │  4,719,104 │ concatenate[0][0] │
│                     │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 32, 32,    │      2,048 │ conv2d_10[0][0]   │
│ (BatchNormalizatio… │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_10       │ (None, 32, 32,    │          0 │ batch_normalizat… │
│ (Activation)        │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_11 (Conv2D)  │ (None, 32, 32,    │  2,359,808 │ activation_10[0]… │
│                     │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 32, 32,    │      2,048 │ conv2d_11[0][0]   │
│ (BatchNormalizatio… │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_11       │ (None, 32, 32,    │          0 │ batch_normalizat… │
│ (Activation)        │ 512)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_transpose_1  │ (None, 64, 64,    │    524,544 │ activation_11[0]… │
│ (Conv2DTranspose)   │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ concatenate_1       │ (None, 64, 64,    │          0 │ conv2d_transpose… │
│ (Concatenate)       │ 512)              │            │ activation_5[0][… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_12 (Conv2D)  │ (None, 64, 64,    │  1,179,904 │ concatenate_1[0]… │
│                     │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 64, 64,    │      1,024 │ conv2d_12[0][0]   │
│ (BatchNormalizatio… │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_12       │ (None, 64, 64,    │          0 │ batch_normalizat… │
│ (Activation)        │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_13 (Conv2D)  │ (None, 64, 64,    │    590,080 │ activation_12[0]… │
│                     │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 64, 64,    │      1,024 │ conv2d_13[0][0]   │
│ (BatchNormalizatio… │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_13       │ (None, 64, 64,    │          0 │ batch_normalizat… │
│ (Activation)        │ 256)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_transpose_2  │ (None, 128, 128,  │    131,200 │ activation_13[0]… │
│ (Conv2DTranspose)   │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ concatenate_2       │ (None, 128, 128,  │          0 │ conv2d_transpose… │
│ (Concatenate)       │ 256)              │            │ activation_3[0][… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_14 (Conv2D)  │ (None, 128, 128,  │    295,040 │ concatenate_2[0]… │
│                     │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 128, 128,  │        512 │ conv2d_14[0][0]   │
│ (BatchNormalizatio… │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_14       │ (None, 128, 128,  │          0 │ batch_normalizat… │
│ (Activation)        │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_15 (Conv2D)  │ (None, 128, 128,  │    147,584 │ activation_14[0]… │
│                     │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 128, 128,  │        512 │ conv2d_15[0][0]   │
│ (BatchNormalizatio… │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_15       │ (None, 128, 128,  │          0 │ batch_normalizat… │
│ (Activation)        │ 128)              │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_transpose_3  │ (None, 256, 256,  │     32,832 │ activation_15[0]… │
│ (Conv2DTranspose)   │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ concatenate_3       │ (None, 256, 256,  │          0 │ conv2d_transpose… │
│ (Concatenate)       │ 128)              │            │ activation_1[0][… │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_16 (Conv2D)  │ (None, 256, 256,  │     73,792 │ concatenate_3[0]… │
│                     │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 256, 256,  │        256 │ conv2d_16[0][0]   │
│ (BatchNormalizatio… │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_16       │ (None, 256, 256,  │          0 │ batch_normalizat… │
│ (Activation)        │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_17 (Conv2D)  │ (None, 256, 256,  │     36,928 │ activation_16[0]… │
│                     │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ batch_normalizatio… │ (None, 256, 256,  │        256 │ conv2d_17[0][0]   │
│ (BatchNormalizatio… │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ activation_17       │ (None, 256, 256,  │          0 │ batch_normalizat… │
│ (Activation)        │ 64)               │            │                   │
├─────────────────────┼───────────────────┼────────────┼───────────────────┤
│ conv2d_18 (Conv2D)  │ (None, 256, 256,  │         65 │ activation_17[0]… │
│                     │ 1)                │            │                   │
└─────────────────────┴───────────────────┴────────────┴───────────────────┘
 Total params: 31,055,297 (118.47 MB)
 Trainable params: 31,043,521 (118.42 MB)
 Non-trainable params: 11,776 (46.00 KB)
In [14]:
optim=optimizers.Adam(learning_rate=learning_rate)

model.compile(optimizer=optim,loss=dice_coefficients_loss,metrics=['accuracy',iou,dice_coefficients])
#model.compile(optimizer=optim,loss=sm.losses.dice_loss,metrics=['accuracy',sm.metrics.iou_score, sm.metrics.f1_score])
In [15]:
checkpoint=callbacks.ModelCheckpoint('unet.keras',save_best_only=True,verbose=1)
early_stopping = callbacks.EarlyStopping(monitor='val_loss', patience=4, mode='min')
In [16]:
history=model.fit(train_gen,epochs=EPOCHS,batch_size=BATCH_SIZE,steps_per_epoch=len(train_df) // BATCH_SIZE,validation_data=val_gen,validation_steps=len(val_df) // BATCH_SIZE,
                  callbacks=[checkpoint])
Found 2514 validated image filenames.
Found 2514 validated image filenames.
Epoch 1/50
2024-08-19 19:20:04.633598: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65536: 3.31777, expected 2.61172
2024-08-19 19:20:04.633664: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65537: 5.10329, expected 4.39724
2024-08-19 19:20:04.633677: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65538: 4.80913, expected 4.10308
2024-08-19 19:20:04.633690: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65544: 5.33366, expected 4.62762
2024-08-19 19:20:04.633704: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65545: 5.00267, expected 4.29663
2024-08-19 19:20:04.633715: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65546: 4.93559, expected 4.22954
2024-08-19 19:20:04.633726: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65547: 5.39283, expected 4.68678
2024-08-19 19:20:04.633737: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65548: 5.61261, expected 4.90657
2024-08-19 19:20:04.633747: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65549: 4.99846, expected 4.29241
2024-08-19 19:20:04.633758: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65555: 5.67579, expected 4.96974
2024-08-19 19:20:04.691542: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[32,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 19:20:04.691590: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 19:20:04.691603: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 19:20:04.691619: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 19:20:04.691631: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 19:20:04.691652: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
2024-08-19 19:20:06.110552: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65536: 3.31777, expected 2.61172
2024-08-19 19:20:06.110612: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65537: 5.10329, expected 4.39724
2024-08-19 19:20:06.110625: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65538: 4.80913, expected 4.10308
2024-08-19 19:20:06.110643: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65544: 5.33366, expected 4.62762
2024-08-19 19:20:06.110662: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65545: 5.00267, expected 4.29663
2024-08-19 19:20:06.110674: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65546: 4.93559, expected 4.22954
2024-08-19 19:20:06.110686: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65547: 5.39283, expected 4.68678
2024-08-19 19:20:06.110697: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65548: 5.61261, expected 4.90657
2024-08-19 19:20:06.110708: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65549: 4.99846, expected 4.29241
2024-08-19 19:20:06.110719: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 65555: 5.67579, expected 4.96974
2024-08-19 19:20:06.169221: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[32,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[32,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 19:20:06.169276: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 19:20:06.169310: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 19:20:06.169325: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 19:20:06.169334: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 19:20:06.169372: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1724095313.624892      69 device_compiler.h:186] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 723ms/step - accuracy: 0.6356 - dice_coefficients: 0.0462 - iou: 0.0238 - loss: -0.0462Found 629 validated image filenames.
Found 629 validated image filenames.

Epoch 1: val_loss improved from inf to -0.02387, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 193s 889ms/step - accuracy: 0.6376 - dice_coefficients: 0.0464 - iou: 0.0239 - loss: -0.0464 - val_accuracy: 0.0223 - val_dice_coefficients: 0.0239 - val_iou: 0.0121 - val_loss: -0.0239
Epoch 2/50
2024-08-19 19:23:04.987323: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 0: 2.6202, expected 1.89179
2024-08-19 19:23:04.987376: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 1: 4.99059, expected 4.26218
2024-08-19 19:23:04.987385: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 2: 4.68753, expected 3.95912
2024-08-19 19:23:04.987394: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 3: 5.1044, expected 4.37599
2024-08-19 19:23:04.987401: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 5: 6.08514, expected 5.35673
2024-08-19 19:23:04.987409: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 6: 5.70028, expected 4.97187
2024-08-19 19:23:04.987417: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 7: 5.51794, expected 4.78953
2024-08-19 19:23:04.987425: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 8: 4.61996, expected 3.89155
2024-08-19 19:23:04.987433: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 9: 4.84939, expected 4.12098
2024-08-19 19:23:04.987440: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 10: 4.96627, expected 4.23786
2024-08-19 19:23:05.020259: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[18,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[18,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 19:23:05.020319: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 19:23:05.020330: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 19:23:05.020337: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 19:23:05.020345: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 19:23:05.020363: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
2024-08-19 19:23:05.727442: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 0: 2.6202, expected 1.89179
2024-08-19 19:23:05.727498: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 1: 4.99059, expected 4.26218
2024-08-19 19:23:05.727507: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 2: 4.68753, expected 3.95912
2024-08-19 19:23:05.727515: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 3: 5.1044, expected 4.37599
2024-08-19 19:23:05.727523: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 5: 6.08514, expected 5.35673
2024-08-19 19:23:05.727531: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 6: 5.70028, expected 4.97187
2024-08-19 19:23:05.727541: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 7: 5.51794, expected 4.78953
2024-08-19 19:23:05.727554: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 8: 4.61996, expected 3.89155
2024-08-19 19:23:05.727562: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 9: 4.84939, expected 4.12098
2024-08-19 19:23:05.727569: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 10: 4.96627, expected 4.23786
2024-08-19 19:23:05.760572: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[18,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[18,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 19:23:05.760617: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 19:23:05.760626: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 19:23:05.760652: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 19:23:05.760659: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 19:23:05.760677: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 724ms/step - accuracy: 0.9644 - dice_coefficients: 0.0916 - iou: 0.0487 - loss: -0.0935
2024-08-19 19:25:06.939338: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 0: 2.99223, expected 2.14902
2024-08-19 19:25:06.939393: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 1: 4.02656, expected 3.18334
2024-08-19 19:25:06.939402: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 2: 4.11124, expected 3.26803
2024-08-19 19:25:06.939410: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 3: 5.67701, expected 4.8338
2024-08-19 19:25:06.939418: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 4: 5.59556, expected 4.75235
2024-08-19 19:25:06.939426: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 5: 5.05851, expected 4.2153
2024-08-19 19:25:06.939434: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 6: 6.15424, expected 5.31103
2024-08-19 19:25:06.939442: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 7: 5.43487, expected 4.59166
2024-08-19 19:25:06.939449: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 8: 4.20502, expected 3.36181
2024-08-19 19:25:06.939457: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 9: 5.17479, expected 4.33157
2024-08-19 19:25:06.977556: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[21,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[21,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 19:25:06.977610: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 19:25:06.977619: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 19:25:06.977626: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 19:25:06.977633: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 19:25:06.977648: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
2024-08-19 19:25:07.774054: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 0: 2.99223, expected 2.14902
2024-08-19 19:25:07.774111: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 1: 4.02656, expected 3.18334
2024-08-19 19:25:07.774121: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 2: 4.11124, expected 3.26803
2024-08-19 19:25:07.774129: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 3: 5.67701, expected 4.8338
2024-08-19 19:25:07.774154: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 4: 5.59556, expected 4.75235
2024-08-19 19:25:07.774163: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 5: 5.05851, expected 4.2153
2024-08-19 19:25:07.774171: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 6: 6.15424, expected 5.31103
2024-08-19 19:25:07.774179: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 7: 5.43487, expected 4.59166
2024-08-19 19:25:07.774187: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 8: 4.20502, expected 3.36181
2024-08-19 19:25:07.774196: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 9: 5.17479, expected 4.33157
2024-08-19 19:25:07.812637: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[21,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[21,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 19:25:07.812690: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 19:25:07.812700: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 19:25:07.812711: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 19:25:07.812723: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 19:25:07.812743: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
Epoch 2: val_loss did not improve from -0.02387
78/78 ━━━━━━━━━━━━━━━━━━━━ 154s 1s/step - accuracy: 0.9644 - dice_coefficients: 0.0918 - iou: 0.0488 - loss: -0.0936 - val_accuracy: 0.1268 - val_dice_coefficients: 0.0123 - val_iou: 0.0062 - val_loss: -0.0124
Epoch 3/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9792 - dice_coefficients: 0.1216 - iou: 0.0655 - loss: -0.1233
Epoch 3: val_loss did not improve from -0.02387
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 781ms/step - accuracy: 0.9792 - dice_coefficients: 0.1217 - iou: 0.0655 - loss: -0.1233 - val_accuracy: 0.7221 - val_dice_coefficients: 0.0135 - val_iou: 0.0069 - val_loss: -0.0135
Epoch 4/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9856 - dice_coefficients: 0.1440 - iou: 0.0783 - loss: -0.1422
Epoch 4: val_loss improved from -0.02387 to -0.03136, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 809ms/step - accuracy: 0.9856 - dice_coefficients: 0.1441 - iou: 0.0784 - loss: -0.1423 - val_accuracy: 0.9894 - val_dice_coefficients: 0.0310 - val_iou: 0.0160 - val_loss: -0.0314
Epoch 5/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9882 - dice_coefficients: 0.1661 - iou: 0.0916 - loss: -0.1685
Epoch 5: val_loss improved from -0.03136 to -0.10888, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 809ms/step - accuracy: 0.9882 - dice_coefficients: 0.1662 - iou: 0.0917 - loss: -0.1686 - val_accuracy: 0.9838 - val_dice_coefficients: 0.1089 - val_iou: 0.0581 - val_loss: -0.1089
Epoch 6/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9899 - dice_coefficients: 0.2008 - iou: 0.1128 - loss: -0.1997
Epoch 6: val_loss did not improve from -0.10888
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9899 - dice_coefficients: 0.2009 - iou: 0.1129 - loss: -0.1998 - val_accuracy: 0.8851 - val_dice_coefficients: 0.1068 - val_iou: 0.0568 - val_loss: -0.1067
Epoch 7/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9916 - dice_coefficients: 0.2184 - iou: 0.1242 - loss: -0.2183
Epoch 7: val_loss improved from -0.10888 to -0.20356, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9916 - dice_coefficients: 0.2187 - iou: 0.1244 - loss: -0.2186 - val_accuracy: 0.9956 - val_dice_coefficients: 0.2020 - val_iou: 0.1138 - val_loss: -0.2036
Epoch 8/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9944 - dice_coefficients: 0.2886 - iou: 0.1708 - loss: -0.2888
Epoch 8: val_loss improved from -0.20356 to -0.28362, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 809ms/step - accuracy: 0.9944 - dice_coefficients: 0.2886 - iou: 0.1709 - loss: -0.2888 - val_accuracy: 0.9906 - val_dice_coefficients: 0.2817 - val_iou: 0.1652 - val_loss: -0.2836
Epoch 9/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9938 - dice_coefficients: 0.3267 - iou: 0.1978 - loss: -0.3265
Epoch 9: val_loss improved from -0.28362 to -0.32794, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9938 - dice_coefficients: 0.3269 - iou: 0.1979 - loss: -0.3267 - val_accuracy: 0.9937 - val_dice_coefficients: 0.3285 - val_iou: 0.1979 - val_loss: -0.3279
Epoch 10/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9947 - dice_coefficients: 0.3666 - iou: 0.2285 - loss: -0.3671
Epoch 10: val_loss improved from -0.32794 to -0.35299, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9947 - dice_coefficients: 0.3668 - iou: 0.2287 - loss: -0.3674 - val_accuracy: 0.9879 - val_dice_coefficients: 0.3528 - val_iou: 0.2163 - val_loss: -0.3530
Epoch 11/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9946 - dice_coefficients: 0.4190 - iou: 0.2689 - loss: -0.4181
Epoch 11: val_loss did not improve from -0.35299
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9946 - dice_coefficients: 0.4192 - iou: 0.2691 - loss: -0.4184 - val_accuracy: 0.9820 - val_dice_coefficients: 0.3117 - val_iou: 0.1873 - val_loss: -0.3114
Epoch 12/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9960 - dice_coefficients: 0.4638 - iou: 0.3074 - loss: -0.4637
Epoch 12: val_loss improved from -0.35299 to -0.46047, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 807ms/step - accuracy: 0.9960 - dice_coefficients: 0.4641 - iou: 0.3076 - loss: -0.4639 - val_accuracy: 0.9954 - val_dice_coefficients: 0.4613 - val_iou: 0.3020 - val_loss: -0.4605
Epoch 13/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9960 - dice_coefficients: 0.5252 - iou: 0.3614 - loss: -0.5248
Epoch 13: val_loss improved from -0.46047 to -0.53829, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9960 - dice_coefficients: 0.5253 - iou: 0.3615 - loss: -0.5249 - val_accuracy: 0.9959 - val_dice_coefficients: 0.5328 - val_iou: 0.3708 - val_loss: -0.5383
Epoch 14/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9971 - dice_coefficients: 0.5977 - iou: 0.4310 - loss: -0.5986
Epoch 14: val_loss improved from -0.53829 to -0.55772, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 807ms/step - accuracy: 0.9971 - dice_coefficients: 0.5977 - iou: 0.4310 - loss: -0.5986 - val_accuracy: 0.9953 - val_dice_coefficients: 0.5568 - val_iou: 0.3890 - val_loss: -0.5577
Epoch 15/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9971 - dice_coefficients: 0.6349 - iou: 0.4696 - loss: -0.6355
Epoch 15: val_loss improved from -0.55772 to -0.62809, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 807ms/step - accuracy: 0.9971 - dice_coefficients: 0.6351 - iou: 0.4698 - loss: -0.6357 - val_accuracy: 0.9967 - val_dice_coefficients: 0.6277 - val_iou: 0.4640 - val_loss: -0.6281
Epoch 16/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9970 - dice_coefficients: 0.6534 - iou: 0.4905 - loss: -0.6528
Epoch 16: val_loss improved from -0.62809 to -0.63443, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9970 - dice_coefficients: 0.6536 - iou: 0.4908 - loss: -0.6530 - val_accuracy: 0.9960 - val_dice_coefficients: 0.6367 - val_iou: 0.4739 - val_loss: -0.6344
Epoch 17/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9974 - dice_coefficients: 0.7021 - iou: 0.5458 - loss: -0.7008
Epoch 17: val_loss improved from -0.63443 to -0.69924, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9974 - dice_coefficients: 0.7022 - iou: 0.5460 - loss: -0.7009 - val_accuracy: 0.9971 - val_dice_coefficients: 0.7010 - val_iou: 0.5437 - val_loss: -0.6992
Epoch 18/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9972 - dice_coefficients: 0.7097 - iou: 0.5562 - loss: -0.7102
Epoch 18: val_loss did not improve from -0.69924
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9972 - dice_coefficients: 0.7098 - iou: 0.5564 - loss: -0.7104 - val_accuracy: 0.9966 - val_dice_coefficients: 0.6958 - val_iou: 0.5375 - val_loss: -0.6974
Epoch 19/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9975 - dice_coefficients: 0.7376 - iou: 0.5893 - loss: -0.7388
Epoch 19: val_loss improved from -0.69924 to -0.72042, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 807ms/step - accuracy: 0.9975 - dice_coefficients: 0.7377 - iou: 0.5894 - loss: -0.7390 - val_accuracy: 0.9968 - val_dice_coefficients: 0.7193 - val_iou: 0.5660 - val_loss: -0.7204
Epoch 20/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9976 - dice_coefficients: 0.7589 - iou: 0.6167 - loss: -0.7596
Epoch 20: val_loss improved from -0.72042 to -0.74811, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9976 - dice_coefficients: 0.7590 - iou: 0.6169 - loss: -0.7597 - val_accuracy: 0.9972 - val_dice_coefficients: 0.7477 - val_iou: 0.6026 - val_loss: -0.7481
Epoch 21/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9976 - dice_coefficients: 0.7936 - iou: 0.6627 - loss: -0.7935
Epoch 21: val_loss improved from -0.74811 to -0.76912, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 809ms/step - accuracy: 0.9976 - dice_coefficients: 0.7936 - iou: 0.6627 - loss: -0.7934 - val_accuracy: 0.9972 - val_dice_coefficients: 0.7691 - val_iou: 0.6306 - val_loss: -0.7691
Epoch 22/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9978 - dice_coefficients: 0.7969 - iou: 0.6696 - loss: -0.7972
Epoch 22: val_loss improved from -0.76912 to -0.77830, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9978 - dice_coefficients: 0.7970 - iou: 0.6697 - loss: -0.7972 - val_accuracy: 0.9974 - val_dice_coefficients: 0.7792 - val_iou: 0.6428 - val_loss: -0.7783
Epoch 23/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9974 - dice_coefficients: 0.7871 - iou: 0.6579 - loss: -0.7866
Epoch 23: val_loss did not improve from -0.77830
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9974 - dice_coefficients: 0.7870 - iou: 0.6576 - loss: -0.7865 - val_accuracy: 0.9958 - val_dice_coefficients: 0.6848 - val_iou: 0.5261 - val_loss: -0.6846
Epoch 24/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9971 - dice_coefficients: 0.7985 - iou: 0.6685 - loss: -0.7984
Epoch 24: val_loss improved from -0.77830 to -0.78324, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9971 - dice_coefficients: 0.7986 - iou: 0.6686 - loss: -0.7985 - val_accuracy: 0.9966 - val_dice_coefficients: 0.7832 - val_iou: 0.6470 - val_loss: -0.7832
Epoch 25/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9979 - dice_coefficients: 0.8155 - iou: 0.6949 - loss: -0.8153
Epoch 25: val_loss did not improve from -0.78324
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9979 - dice_coefficients: 0.8155 - iou: 0.6949 - loss: -0.8153 - val_accuracy: 0.9966 - val_dice_coefficients: 0.7598 - val_iou: 0.6179 - val_loss: -0.7635
Epoch 26/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9972 - dice_coefficients: 0.8092 - iou: 0.6857 - loss: -0.8091
Epoch 26: val_loss did not improve from -0.78324
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9972 - dice_coefficients: 0.8093 - iou: 0.6858 - loss: -0.8092 - val_accuracy: 0.9968 - val_dice_coefficients: 0.7754 - val_iou: 0.6443 - val_loss: -0.7742
Epoch 27/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9977 - dice_coefficients: 0.8388 - iou: 0.7252 - loss: -0.8386
Epoch 27: val_loss improved from -0.78324 to -0.79541, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9977 - dice_coefficients: 0.8389 - iou: 0.7254 - loss: -0.8386 - val_accuracy: 0.9974 - val_dice_coefficients: 0.7967 - val_iou: 0.6674 - val_loss: -0.7954
Epoch 28/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9977 - dice_coefficients: 0.8295 - iou: 0.7135 - loss: -0.8297
Epoch 28: val_loss improved from -0.79541 to -0.82919, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9977 - dice_coefficients: 0.8296 - iou: 0.7137 - loss: -0.8298 - val_accuracy: 0.9975 - val_dice_coefficients: 0.8285 - val_iou: 0.7120 - val_loss: -0.8292
Epoch 29/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 719ms/step - accuracy: 0.9978 - dice_coefficients: 0.8600 - iou: 0.7566 - loss: -0.8597
Epoch 29: val_loss did not improve from -0.82919
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9978 - dice_coefficients: 0.8600 - iou: 0.7567 - loss: -0.8597 - val_accuracy: 0.9972 - val_dice_coefficients: 0.8204 - val_iou: 0.7000 - val_loss: -0.8204
Epoch 30/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9979 - dice_coefficients: 0.8591 - iou: 0.7550 - loss: -0.8591
Epoch 30: val_loss improved from -0.82919 to -0.84014, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9979 - dice_coefficients: 0.8591 - iou: 0.7551 - loss: -0.8591 - val_accuracy: 0.9975 - val_dice_coefficients: 0.8379 - val_iou: 0.7237 - val_loss: -0.8401
Epoch 31/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9981 - dice_coefficients: 0.8649 - iou: 0.7648 - loss: -0.8647
Epoch 31: val_loss did not improve from -0.84014
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9981 - dice_coefficients: 0.8647 - iou: 0.7646 - loss: -0.8646 - val_accuracy: 0.9970 - val_dice_coefficients: 0.8213 - val_iou: 0.6994 - val_loss: -0.8206
Epoch 32/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9978 - dice_coefficients: 0.8652 - iou: 0.7657 - loss: -0.8649
Epoch 32: val_loss did not improve from -0.84014
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9978 - dice_coefficients: 0.8652 - iou: 0.7657 - loss: -0.8650 - val_accuracy: 0.9972 - val_dice_coefficients: 0.8293 - val_iou: 0.7115 - val_loss: -0.8283
Epoch 33/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9975 - dice_coefficients: 0.8399 - iou: 0.7285 - loss: -0.8396
Epoch 33: val_loss improved from -0.84014 to -0.84324, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9975 - dice_coefficients: 0.8400 - iou: 0.7287 - loss: -0.8398 - val_accuracy: 0.9973 - val_dice_coefficients: 0.8414 - val_iou: 0.7295 - val_loss: -0.8432
Epoch 34/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9980 - dice_coefficients: 0.8583 - iou: 0.7551 - loss: -0.8581
Epoch 34: val_loss did not improve from -0.84324
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9980 - dice_coefficients: 0.8584 - iou: 0.7552 - loss: -0.8581 - val_accuracy: 0.9972 - val_dice_coefficients: 0.8166 - val_iou: 0.6932 - val_loss: -0.8180
Epoch 35/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9977 - dice_coefficients: 0.8695 - iou: 0.7722 - loss: -0.8696
Epoch 35: val_loss improved from -0.84324 to -0.85546, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 809ms/step - accuracy: 0.9977 - dice_coefficients: 0.8695 - iou: 0.7723 - loss: -0.8696 - val_accuracy: 0.9977 - val_dice_coefficients: 0.8552 - val_iou: 0.7499 - val_loss: -0.8555
Epoch 36/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9981 - dice_coefficients: 0.8846 - iou: 0.7946 - loss: -0.8844
Epoch 36: val_loss did not improve from -0.85546
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9981 - dice_coefficients: 0.8846 - iou: 0.7946 - loss: -0.8844 - val_accuracy: 0.9974 - val_dice_coefficients: 0.8340 - val_iou: 0.7213 - val_loss: -0.8338
Epoch 37/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9980 - dice_coefficients: 0.8846 - iou: 0.7955 - loss: -0.8847
Epoch 37: val_loss did not improve from -0.85546
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9980 - dice_coefficients: 0.8846 - iou: 0.7954 - loss: -0.8847 - val_accuracy: 0.9973 - val_dice_coefficients: 0.8502 - val_iou: 0.7433 - val_loss: -0.8497
Epoch 38/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9982 - dice_coefficients: 0.8780 - iou: 0.7876 - loss: -0.8781
Epoch 38: val_loss did not improve from -0.85546
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9982 - dice_coefficients: 0.8781 - iou: 0.7878 - loss: -0.8783 - val_accuracy: 0.9977 - val_dice_coefficients: 0.8473 - val_iou: 0.7445 - val_loss: -0.8466
Epoch 39/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9981 - dice_coefficients: 0.8871 - iou: 0.8002 - loss: -0.8872
Epoch 39: val_loss did not improve from -0.85546
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9981 - dice_coefficients: 0.8872 - iou: 0.8004 - loss: -0.8872 - val_accuracy: 0.9975 - val_dice_coefficients: 0.8532 - val_iou: 0.7484 - val_loss: -0.8523
Epoch 40/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9980 - dice_coefficients: 0.8820 - iou: 0.7923 - loss: -0.8819
Epoch 40: val_loss improved from -0.85546 to -0.86457, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9980 - dice_coefficients: 0.8821 - iou: 0.7925 - loss: -0.8820 - val_accuracy: 0.9974 - val_dice_coefficients: 0.8646 - val_iou: 0.7630 - val_loss: -0.8646
Epoch 41/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9979 - dice_coefficients: 0.8584 - iou: 0.7613 - loss: -0.8582
Epoch 41: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 784ms/step - accuracy: 0.9979 - dice_coefficients: 0.8585 - iou: 0.7615 - loss: -0.8584 - val_accuracy: 0.9977 - val_dice_coefficients: 0.8594 - val_iou: 0.7566 - val_loss: -0.8594
Epoch 42/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9981 - dice_coefficients: 0.8876 - iou: 0.8009 - loss: -0.8879
Epoch 42: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9981 - dice_coefficients: 0.8877 - iou: 0.8010 - loss: -0.8879 - val_accuracy: 0.9973 - val_dice_coefficients: 0.8553 - val_iou: 0.7500 - val_loss: -0.8554
Epoch 43/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9983 - dice_coefficients: 0.9013 - iou: 0.8220 - loss: -0.9011
Epoch 43: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9983 - dice_coefficients: 0.9012 - iou: 0.8220 - loss: -0.9011 - val_accuracy: 0.9976 - val_dice_coefficients: 0.8621 - val_iou: 0.7604 - val_loss: -0.8623
Epoch 44/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9981 - dice_coefficients: 0.8965 - iou: 0.8141 - loss: -0.8964
Epoch 44: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9981 - dice_coefficients: 0.8965 - iou: 0.8141 - loss: -0.8964 - val_accuracy: 0.9976 - val_dice_coefficients: 0.8623 - val_iou: 0.7602 - val_loss: -0.8612
Epoch 45/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9982 - dice_coefficients: 0.8905 - iou: 0.8057 - loss: -0.8905
Epoch 45: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9982 - dice_coefficients: 0.8905 - iou: 0.8057 - loss: -0.8905 - val_accuracy: 0.9976 - val_dice_coefficients: 0.8529 - val_iou: 0.7493 - val_loss: -0.8544
Epoch 46/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9983 - dice_coefficients: 0.9005 - iou: 0.8204 - loss: -0.9005
Epoch 46: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9983 - dice_coefficients: 0.9005 - iou: 0.8204 - loss: -0.9005 - val_accuracy: 0.9972 - val_dice_coefficients: 0.8514 - val_iou: 0.7467 - val_loss: -0.8503
Epoch 47/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9981 - dice_coefficients: 0.8885 - iou: 0.8022 - loss: -0.8884
Epoch 47: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 782ms/step - accuracy: 0.9981 - dice_coefficients: 0.8886 - iou: 0.8024 - loss: -0.8885 - val_accuracy: 0.9977 - val_dice_coefficients: 0.8522 - val_iou: 0.7477 - val_loss: -0.8521
Epoch 48/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 721ms/step - accuracy: 0.9982 - dice_coefficients: 0.8998 - iou: 0.8200 - loss: -0.8999
Epoch 48: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9982 - dice_coefficients: 0.8996 - iou: 0.8198 - loss: -0.8998 - val_accuracy: 0.9973 - val_dice_coefficients: 0.8511 - val_iou: 0.7466 - val_loss: -0.8499
Epoch 49/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 720ms/step - accuracy: 0.9979 - dice_coefficients: 0.8932 - iou: 0.8088 - loss: -0.8931
Epoch 49: val_loss did not improve from -0.86457
78/78 ━━━━━━━━━━━━━━━━━━━━ 61s 783ms/step - accuracy: 0.9979 - dice_coefficients: 0.8931 - iou: 0.8087 - loss: -0.8931 - val_accuracy: 0.9974 - val_dice_coefficients: 0.8561 - val_iou: 0.7514 - val_loss: -0.8549
Epoch 50/50
78/78 ━━━━━━━━━━━━━━━━━━━━ 0s 721ms/step - accuracy: 0.9981 - dice_coefficients: 0.8876 - iou: 0.8012 - loss: -0.8877
Epoch 50: val_loss improved from -0.86457 to -0.87122, saving model to unet.keras
78/78 ━━━━━━━━━━━━━━━━━━━━ 63s 808ms/step - accuracy: 0.9981 - dice_coefficients: 0.8877 - iou: 0.8013 - loss: -0.8878 - val_accuracy: 0.9978 - val_dice_coefficients: 0.8725 - val_iou: 0.7759 - val_loss: -0.8712
In [17]:
!mkdir saved_model
/opt/conda/lib/python3.10/pty.py:89: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.
  pid, fd = os.forkpty()
In [18]:
# Save the entire model in the SavedModel format
model=tf.keras.models.load_model('unet.keras',custom_objects={'dice_coefficients_loss': dice_coefficients_loss,'iou':iou,'dice_coefficients':dice_coefficients})
In [19]:
#Evaluate The Model
history_post_training = history.history

train_dice_coeff_list = history_post_training['dice_coefficients']
test_dice_coeff_list = history_post_training['val_dice_coefficients']

train_jaccard_list = history_post_training['iou']
test_jaccard_list = history_post_training['val_iou']

train_loss_list = history_post_training['loss']
test_loss_list = history_post_training['val_loss']

plt.figure(1)
plt.plot(test_loss_list, 'b-')
plt.plot(train_loss_list, 'r-')

plt.xlabel('iterations')
plt.ylabel('loss')
plt.title('loss graph', fontsize=12)

plt.figure(2)
plt.plot(train_dice_coeff_list, 'b-')
plt.plot(test_dice_coeff_list, 'r-')

plt.xlabel('iterations')
plt.ylabel('accuracy')
plt.title('Accuracy graph', fontsize=12)
plt.show()
In [20]:
def get_prediction(df):
# Select 8 random indices from the DataFrame
    if len(df) == 0:
        raise ValueError("The DataFrame is empty")
    
    # Select a random index from the DataFrame
    random_index = np.random.randint(0,len(df))
    image_path = df.iloc[random_index]['file_path']
    mask_path = df.iloc[random_index]['mask']

    # Read and preprocess the image
    image = cv2.imread(image_path)
    if image is None:
        raise FileNotFoundError(f"Image not found: {image_path}")
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = image / 255.0
    image = cv2.resize(image, (w, h))

    # Read and preprocess the mask
    mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
    if mask is None:
        raise FileNotFoundError(f"Mask not found: {mask_path}")
    mask = mask / 255.0
    mask = cv2.resize(mask, (w, h))

    # Prepare image for model prediction
    image_input = np.expand_dims(image, axis=0)
    
    # Get predicted mask
    pred_mask = model.predict(image_input)
    pred_mask = np.squeeze(pred_mask, axis=0)>0.5  # Remove the batch dimension

    # Plot the image, true mask, and predicted mask
    fig, axes = plt.subplots(1, 3, figsize=(15, 5))
    fig.suptitle('Brain Image with True Mask and Predicted Mask', fontsize=16)

    axes[0].imshow(image)
    axes[0].set_title('Image')
    axes[0].axis('off')

    axes[1].imshow(mask, cmap='gray')
    axes[1].set_title('True Mask')
    axes[1].axis('off')

    axes[2].imshow(pred_mask, cmap='gray')
    axes[2].set_title('Predicted Mask')
    axes[2].axis('off')
    
    plt.tight_layout()
    plt.subplots_adjust(top=0.85)  # Adjust title position
    plt.show()
In [21]:
get_prediction(test_df)
2024-08-19 20:15:18.431596: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 0: 2.86272, expected 2.0465
2024-08-19 20:15:18.431662: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 1: 4.04967, expected 3.23345
2024-08-19 20:15:18.431677: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 2: 4.4326, expected 3.61638
2024-08-19 20:15:18.431689: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 3: 5.46868, expected 4.65247
2024-08-19 20:15:18.431705: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 4: 5.37814, expected 4.56193
2024-08-19 20:15:18.431717: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 5: 5.18374, expected 4.36753
2024-08-19 20:15:18.431728: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 6: 5.54872, expected 4.7325
2024-08-19 20:15:18.431738: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 7: 5.78654, expected 4.97032
2024-08-19 20:15:18.431748: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 8: 4.0901, expected 3.27388
2024-08-19 20:15:18.431760: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 9: 5.11265, expected 4.29643
2024-08-19 20:15:18.431784: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[1,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[1,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 20:15:18.431796: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 20:15:18.431806: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 20:15:18.431815: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 20:15:18.431823: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 20:15:18.431842: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
2024-08-19 20:15:18.472382: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 0: 2.86272, expected 2.0465
2024-08-19 20:15:18.472429: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 1: 4.04967, expected 3.23345
2024-08-19 20:15:18.472439: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 2: 4.4326, expected 3.61638
2024-08-19 20:15:18.472447: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 3: 5.46868, expected 4.65247
2024-08-19 20:15:18.472455: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 4: 5.37814, expected 4.56193
2024-08-19 20:15:18.472462: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 5: 5.18374, expected 4.36753
2024-08-19 20:15:18.472470: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 6: 5.54872, expected 4.7325
2024-08-19 20:15:18.472478: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 7: 5.78654, expected 4.97032
2024-08-19 20:15:18.472486: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 8: 4.0901, expected 3.27388
2024-08-19 20:15:18.472493: E external/local_xla/xla/service/gpu/buffer_comparator.cc:1137] Difference at 9: 5.11265, expected 4.29643
2024-08-19 20:15:18.472508: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:705] Results mismatch between different convolution algorithms. This is likely a bug/unexpected loss of precision in cudnn.
(f32[1,64,256,256]{3,2,1,0}, u8[0]{0}) custom-call(f32[1,3,256,256]{3,2,1,0}, f32[64,3,3,3]{3,2,1,0}, f32[64]{0}), window={size=3x3 pad=1_1x1_1}, dim_labels=bf01_oi01->bf01, custom_call_target="__cudnn$convBiasActivationForward", backend_config={"conv_result_scale":1,"activation_mode":"kNone","side_input_scale":0,"leakyrelu_alpha":0} for eng20{k2=1,k4=1,k5=1,k6=0,k7=0} vs eng15{k5=1,k6=0,k7=1,k10=1}
2024-08-19 20:15:18.472519: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:270] Device: Tesla P100-PCIE-16GB
2024-08-19 20:15:18.472526: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:271] Platform: Compute Capability 6.0
2024-08-19 20:15:18.472533: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:272] Driver: 12040 (550.90.7)
2024-08-19 20:15:18.472541: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:273] Runtime: <undefined>
2024-08-19 20:15:18.472557: E external/local_xla/xla/service/gpu/conv_algorithm_picker.cc:280] cudnn version: 8.9.0
1/1 ━━━━━━━━━━━━━━━━━━━━ 4s 4s/step

Thank You¶

In [ ]: